//
//  MZRecommendPhotosViewController.m
//  OurTourism
//
//  Created by 孟镇 on 15/11/28.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "MZRecommendPhotosViewController.h"
#import "Header.h"
#import "MZPhotoModel.h"
#import "MZSinglePhotoCollectionViewCell.h"
#import "YBTraveldetialViewController.h"

@interface MZRecommendPhotosViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, strong) UIView *naviView;
@property(nonatomic, strong) UIView *normalView;
@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) NSMutableArray *dataArr;
@property(nonatomic, strong) MBProgressHUD *HUB;
@property(nonatomic, strong) NSMutableArray *imageArr;
@property(nonatomic, strong) NSMutableArray *pullArr;
@property(nonatomic, strong) NSString *next_start;
@property(nonatomic, assign) BOOL isRefresh;
@property(nonatomic, strong) UICollectionView *photoCollectionView;
@property(nonatomic, strong) UIView *photoView;
@property(nonatomic, strong) UIView *photoTitleView; //黑色背景标题View
@property(nonatomic, strong) UILabel *photoTitleLabel;
@property(nonatomic, strong) UIView *photoTabBarView; //黑色背景tabbar View
@property(nonatomic, assign) NSInteger index;

@end

@implementation MZRecommendPhotosViewController

//#pragma mark - 白色status bar
//- (UIStatusBarStyle)preferredStatusBarStyle
//{
//    return UIStatusBarStyleLightContent;
//}

//#pragma mark - 隐藏状态栏
//- (BOOL)prefersStatusBarHidden
//{
//    return YES;
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    [self createPhotoNaviView];
    
    [self createPhotoView];
    
    self.dataArr = [NSMutableArray array];
    self.imageArr = [NSMutableArray array];
    self.pullArr = [NSMutableArray array];
    
    self.next_start = @"0";
    self.isRefresh = YES;
    
    self.normalView = [[UIView alloc] initWithFrame:self.view.frame];
    self.normalView.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    [self.view addSubview:self.normalView];
    
    [self createCollectionView];
    [self setupRefresh];
    
    for (NSInteger i = 1; i <= 4; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld", i]];
        [self.pullArr addObject:image];
    }
    UIImage *img = [UIImage imageNamed:@"0"];
    [self.imageArr addObject:img];
    
    self.HUB = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.HUB show:YES];
    self.HUB.labelText = @"正在加载";
    
    [self createNavi];
}

#pragma mark - 创建单独图片navi的view
- (void)createPhotoNaviView
{
    //黑色背景
    self.photoView = [[UIView alloc] initWithFrame:self.view.frame];
    self.photoView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.photoView];
    self.photoTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.photoTitleView.backgroundColor = [UIColor blackColor];
    [self.photoView addSubview:self.photoTitleView];
    //黑色背景消失Button
    UIButton *dismissBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [dismissBtn setImage:[UIImage imageNamed:@"iconfont-iconfonticonfontjiantouzuo11@2x"] forState:UIControlStateNormal];
    dismissBtn.frame = CGRectMake(10, 25, 25, 25);
    [dismissBtn addTarget:self action:@selector(dismissAction) forControlEvents:UIControlEventTouchUpInside];
    [self.photoTitleView addSubview:dismissBtn];
    //photoTitleLabel
    self.photoTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2  - 120, 25, 240, 25)];
    self.photoTitleLabel.textColor = [UIColor whiteColor];
    self.photoTitleLabel.textAlignment = NSTextAlignmentCenter;
    self.photoTitleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    [self.photoTitleView addSubview:self.photoTitleLabel];
    //查看游记按钮
    UIButton *findNoteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    findNoteBtn.frame = CGRectMake(WIDTH - 60, 26, 50, 25);
    [findNoteBtn setTitle:@"查看游记" forState:UIControlStateNormal];
    [findNoteBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    findNoteBtn.layer.borderWidth = 0.5;
    findNoteBtn.layer.cornerRadius = 10;
    findNoteBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    findNoteBtn.titleLabel.font = [UIFont systemFontOfSize:11];
    [findNoteBtn addTarget:self action:@selector(findNoteAction) forControlEvents:UIControlEventTouchUpInside];
    [self.photoTitleView addSubview:findNoteBtn];
}

#pragma mark - 创建单独图片view
- (void)createPhotoView
{
#pragma mark - 创建单个图片collectionView
    [self createPhotoCollectionView];
    
    //单个图片下方View
    self.photoTabBarView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGTH - 50, WIDTH, 50)];
    self.photoTabBarView.backgroundColor = [UIColor blackColor];
    self.photoTabBarView.alpha = .7;
    [self.photoView addSubview:self.photoTabBarView];
    //收藏按钮
    UIButton *likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [likeBtn setImage:[UIImage imageNamed:@"whiteHeart"] forState:UIControlStateNormal];
    likeBtn.frame = CGRectMake(WIDTH / 3 / 2 - 15, 10, 30, 30);
    [likeBtn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.photoTabBarView addSubview:likeBtn];
    //分割线
    UIView *lineView = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 3, 10, 1, 30)];
    lineView.backgroundColor = [UIColor whiteColor];
    [self.photoTabBarView addSubview:lineView];
    //评论按钮
    UIButton *forumBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [forumBtn setImage:[UIImage imageNamed:@"iconfont-pinglun"] forState:UIControlStateNormal];
    forumBtn.frame = CGRectMake(WIDTH / 3 / 2 - 15 + WIDTH / 3, 10, 30, 30);
    [forumBtn addTarget:self action:@selector(forumAction) forControlEvents:UIControlEventTouchUpInside];
    [self.photoTabBarView addSubview:forumBtn];
    //分割线
    UIView *otherLineView = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 3 * 2, 10, 1, 30)];
    otherLineView.backgroundColor = [UIColor whiteColor];
    [self.photoTabBarView addSubview:otherLineView];
    //分享按钮
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareBtn setImage:[UIImage imageNamed:@"iconfont-fenxiang"] forState:UIControlStateNormal];
    shareBtn.frame = CGRectMake(WIDTH / 3 / 2 - 15 + WIDTH / 3 * 2, 10, 30, 30);
    [shareBtn addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    [self.photoTabBarView addSubview:shareBtn];
}

#pragma mark - 创建假的navi
- (void)createNavi
{
    self.naviView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.naviView.backgroundColor = [UIColor colorWithRed:62 / 255. green:176 / 255. blue:192 / 255. alpha:1];
    [self.normalView addSubview:self.naviView];
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(10, 25, 25, 25);
    [backBtn setImage:[UIImage imageNamed:@"iconfont-iconfonticonfontjiantouzuo11@2x"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    [self.normalView addSubview:backBtn];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 100, 26, 200, 25)];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = [NSString stringWithFormat:@"%@图片", self.tempModel.name];
    titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:19];
    [self.normalView addSubview:titleLabel];
}

#pragma mark - 创建collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((WIDTH - 10) / 3, (WIDTH - 10) / 3);
    layout.sectionInset = UIEdgeInsetsMake(5, 0, 5, 0);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGTH - 64) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    [self.normalView addSubview:self.collectionView];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"photoItem"];
}
- (void)createPhotoCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(WIDTH, HEIGTH - 64);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    
    self.photoCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGTH - 64) collectionViewLayout:layout];
    self.photoCollectionView.pagingEnabled = YES;
    self.photoCollectionView.delegate = self;
    self.photoCollectionView.dataSource = self;
//    self.photoCollectionView.bounces = NO;
//    self.photoCollectionView.showsVerticalScrollIndicator = NO;
    self.photoCollectionView.backgroundColor = [UIColor blackColor];
    [self.photoView addSubview:self.photoCollectionView];
    [self.photoCollectionView registerClass:[MZSinglePhotoCollectionViewCell class] forCellWithReuseIdentifier:@"singlePhotoItem"];
}

#pragma mark - 准备数据
- (void)prepareData
{
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com//destination/place/%@/%@/photos/?gallery_mode=1&count=18&start=%@", self.tempModel.type, self.tempModel.valueId, self.next_start];
    
    [MZNetHandle getUrl:str cookie:nil result:^(id result) {
        
        if (self.isRefresh) {
            
            [self.dataArr removeAllObjects];
        }
       
        NSArray *arr = result[@"items"];
        [self.dataArr addObjectsFromArray:[MZPhotoModel transformWithArray:arr]];
        
        self.next_start = result[@"next_start"];
        
        [self.collectionView reloadData];
        [self.photoCollectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        [self.photoCollectionView.mj_footer endRefreshing];
        [self.HUB removeFromSuperview];
    }];
}

#pragma mark - 刷新加载
- (void)setupRefresh
{
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    // 设置普通状态的动画图片
    [header setImages:self.imageArr forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [header setImages:self.imageArr forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    [header setImages:self.pullArr forState:MJRefreshStateRefreshing];
    self.collectionView.mj_header = header;
    [self.collectionView.mj_header beginRefreshing];
    
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    // 设置普通状态的动画图片
    [footer setImages:self.imageArr forState:MJRefreshStateIdle];
    // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
    [footer setImages:self.imageArr forState:MJRefreshStatePulling];
    // 设置正在刷新状态的动画图片
    [footer setImages:self.pullArr forState:MJRefreshStateRefreshing];
    self.collectionView.mj_footer = footer;
    
//    MJRefreshFooter *photoFooter = [MJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
//    self.photoCollectionView.mj_footer = photoFooter;
    //self.photoCollectionView.footer = [MJRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
}

- (void)headerRefresh
{
    self.isRefresh = YES;
    self.next_start = @"0";
    [self prepareData];
}

- (void)footerRefresh
{
    self.isRefresh = NO;
    [self prepareData];
}

#pragma mark - collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.collectionView) {
        
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photoItem" forIndexPath:indexPath];
        UIImageView *img = [[UIImageView alloc] initWithFrame:cell.contentView.frame];
        [img sd_setImageWithURL:[NSURL URLWithString:[self.dataArr[indexPath.item] photo]] placeholderImage:[UIImage imageNamed:@"collectionItem"]];
        img.contentMode = UIViewContentModeScaleAspectFill;
        img.clipsToBounds = YES;
        cell.backgroundView = img;
        return cell;
    }else{
        
        MZSinglePhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"singlePhotoItem" forIndexPath:indexPath];
        cell.tempModel = self.dataArr[indexPath.item];
        return cell;
    }
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == self.collectionView) {
        
        [collectionView deselectItemAtIndexPath:indexPath animated:YES];
        
        self.photoCollectionView.contentOffset = CGPointMake(WIDTH * indexPath.item, 0);
        self.photoTitleLabel.text = [self.dataArr[indexPath.item] trip_name];
        
        self.index = indexPath.item;
        
        [UIView animateWithDuration:0.3 animations:^{

            self.normalView.alpha = 0;
            self.photoView.alpha = 1;
        }];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger i = scrollView.contentOffset.x / WIDTH;
    self.photoTitleLabel.text = [self.dataArr[i] trip_name];
}

- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)dismissAction
{
    [self.photoCollectionView removeFromSuperview];
    [self.photoTabBarView removeFromSuperview];
    [self createPhotoView];
    [UIView animateWithDuration:0.3 animations:^{
        
        self.normalView.alpha = 1;
        self.photoView.alpha = 0;
        
    }];
}

- (void)findNoteAction
{
    [self.photoCollectionView removeFromSuperview];
    [self.photoTabBarView removeFromSuperview];
    [self createPhotoView];
    
    YBTraveldetialViewController *tdVC = [[YBTraveldetialViewController alloc] init];
    
    tdVC.valueId = [self.dataArr[self.index] trip_id];
    tdVC.whoPush = @"mz";
    
    [self presentViewController:tdVC animated:YES completion:nil];
}

- (void)likeAction:(UIButton *)sender
{
    
}

- (void)forumAction
{
    
}

- (void)shareAction
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
